<template>
  <div class="login-page">
    <div class="login-box">
      <img class="logo" src="http://www.jixingyun.cn/zb_users/theme/tpure/style/images/logo.svg" alt="logo" />
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef" class="form" label-position="top">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="loginForm.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="loginForm.remember">保持登录</el-checkbox>
        </el-form-item>
        <el-button type="primary" class="login-button" :loading="loading" @click="submitLogin">登录</el-button>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import request from '@/utils/request';
import MD5 from 'crypto-js/md5'

const router = useRouter();
const loginFormRef = ref(null);
const loading = ref(false);

const loginForm = reactive({
  username: '',
  password: '',
  remember: false,
});

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应为3到20个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 30, message: '密码长度应为6到30个字符', trigger: 'blur' }
  ]
};

// 登录方法
const submitLogin = () => {
  if (!loginFormRef.value) return;
  
  loginFormRef.value.validate(async (valid) => {
    if (!valid) {
      return false;
    }
    
    loading.value = true;
    const encryptedPassword = MD5(loginForm.password).toString();
    try {
      const response = await request.post('/api/login', {
        username: loginForm.username,
        password: encryptedPassword
      });
      
      const { data } = response;
      console.log(data);
      if (data.code === 200) {
        // 登录成功，存储token
        console.log(data.data);
        localStorage.setItem('token', data.data);
        
        // 如果选择记住我，则存储用户名
        if (loginForm.remember) {
          localStorage.setItem('username', loginForm.username);
        } else {
          localStorage.removeItem('username');
        }
        
        ElMessage.success('登录成功');
        
        // 跳转到首页或者之前记录的页面
        router.push('home/management');
      } else {
        ElMessage.error(data.message || '登录失败');
      }
    } catch (error) {
      console.error('登录异常:', error);
      ElMessage.error('登录异常，请稍后重试');
    } finally {
      loading.value = false;
    }
  });
};

// 页面加载时检查是否有存储的用户名
const checkSavedUsername = () => {
  const savedUsername = localStorage.getItem('username');
  if (savedUsername) {
    loginForm.username = savedUsername;
    loginForm.remember = true;
  }
};

// 页面初始化时执行
checkSavedUsername();
</script>

<style scoped>
.login-page {
  height: 100vh;
  background: url('http://www.jixingyun.cn/zb_users/theme/tpure/style/images/banner.jpg') no-repeat center center;
  background-size: cover;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-box {
  background-color: white;
  padding: 40px;
  border-radius: 10px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
  width: 360px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.logo {
  width: 120px;
  margin-bottom: 20px;
}

.form {
  width: 100%;
}

.login-button {
  width: 100%;
  margin-top: 15px;
  height: 40px;
}

body {
  margin: 0 !important;
  padding: 0;
}
</style>